#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.api import users

def doAuthGoogleUser(handler):
	user = users.get_current_user()
	if user:
		greeting = ("Welcome, %s! (<a href=\"%s\">sign out</a>)" %
					(user.nickname(), users.create_logout_url("/")))
	else:
		greeting = ("<a href=\"%s\">Sign in or register</a>." %
					users.create_login_url("/"))

	handler.response.out.write("<html><body>%s</body></html>" % greeting)
	return True;

def doRender(handler, tname='list.html', values={}):
	templatePath = os.path.join(os.path.dirname(__file__), 'templates/'+tname)
	if not os.path.isfile(templatePath):
		return False
	
	# Make a copy of the dictionary and add the path
	newval = dict(values)
	newval['path'] = handler.request.path
	
	# Render template
	strOutput = template.render(templatePath, newval)
	handler.response.out.write(strOutput)
	return True;	

		
class MainHandler(webapp.RequestHandler):
    def get(self):
		doRender(self, 'list.html')

class LoginHandler(webapp.RequestHandler):
	def get(self):
		doAuthGoogleUser(self)

class ManageHandler(webapp.RequestHandler):
    def get(self):
		doRender(self, 'manage.html')


class AddHandler(webapp.RequestHandler):
    def get(self):
		self.response.out.write('AddHandler')


class DelHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('DelHandler')

		
class EditHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('EditHandler')

		
class RentHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('RentHandler')


class ReturnHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('ReturnHandler')


def main():
    application = webapp.WSGIApplication([
		('/', MainHandler),
		('/login', LoginHandler),
		('/manage', ManageHandler),
		('/add', AddHandler),
		('/del', DelHandler),
		('/edit', EditHandler),
		('/rent', RentHandler),
		('/return', ReturnHandler)],
		debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
